<?php
/*
 * @Author: your name
 * @Date: 2020-07-04 18:25:04
 * @LastEditTime: 2020-09-30 17:52:54
 * @LastEditors: 天羽落星辰
 * @Description: In User Settings Edit
 * @FilePath: \WWW\frx\application\api\controller\Dingshirenwu.php
 */

namespace app\api\controller;

use think\Db;
use app\common\model\Rtp;

use app\admin\model\Order;
use app\common\model\User;
use app\common\controller\Api;
use app\admin\model\UserScoreLog;
use app\admin\model\QianbaoSysetmLog;

/**
 * 定时计划
 */
class Dingshirenwu extends Api
{

    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _empty($name)
    {
        die('非法访问！你的IP已被监控！');
    }
    public function index()
    {
        die('非法访问！');
    }


    /*========================================================================*/

    /**
     * 查询是否到账
     */
    public function success_not_accoun()
    {
        set_time_limit(0);
        $lists = db('qianbao_sysetm_log')->where('type', 2)->where('zt', '4')->limit('30')->select();
        if ($lists) {
            foreach ($lists as $v) {
                //查询交易是否到账 0查询失败，1交易成功，2交易失败，3审核中 4处理中 5 提币失败（重新提币or 驳回提币）
                $receipt = erc_daozhang($v['hash']);
                // 失败
                if ($receipt['zt'] == 2) {
                    // db('qianbao_sysetm_log')->where('id',$v['id'])->update(['zt' => '5','memo' => '提币转账失败']);
                    QianbaoSysetmLog::update(['id' => $v['id'], 'zt' => '2', 'memo' => '提币转账失败']);
                    echo ($v['id'] . $receipt['msg'] . $v['hash'] . PHP_EOL);
                    // 成功
                } elseif ($receipt['zt'] == 1) {
                    QianbaoSysetmLog::update(['id' => $v['id'], 'zt' => '1', 'memo' => '提币成功']);
                    // db('qianbao_sysetm_log')->where('id',$v['id'])->update(['zt' => '1','memo' => '提币成功']);
                    echo ($v['id'] . $receipt['msg'] . PHP_EOL);
                } else {
                    echo ($v['id'] . $receipt['msg'] . $v['hash'] . PHP_EOL);
                }
            }
        } else {
            echo '暂无提币数据' . PHP_EOL;
        }
    }

    /**
     * 查询并更新货币涨幅价格
     *
     */
    public function moneyprice()
    {
        $rtp = new Rtp();
        $rtps = config('site.rtps');
        $rtps = explode(',', $rtps);
        // 货币循环查询走势
        foreach ($rtps as $k => $v) {
            $data = file_get_contents(sprintf(config('rtp')['kline%s'], strtolower($v . 'usdt')));
            $data = json_decode($data, true);
            if ($data['status'] != 'ok') {
                echo ('接口请求失败');
            }
            $today_arr = reset($data['data']);
            $close = $today_arr['close'];
            $open = $today_arr['open'];
            $quotation['range'] = round(($open - $close) / $open * 100, 2); // 涨幅
            $quotation['price'] = $close; // 美元价格
            $quotation['vol'] = '24h量 ' . round($today_arr['vol'], 2); // 美元价格
            $quotation['type'] = $v . '/USDT';
            $quotation['bizhong'] = $v;

            $row = $rtp->field(true)->where(['bizhong' => $v])->find();
            if ($row) {
                $return = $rtp::where('id',$row['id'])->update($quotation);
                if ($return) {
                    echo ($v . '更新成功' . PHP_EOL);
                } else {
                    echo ($v . '更新失败，数据未变动' . PHP_EOL);
                }
            } else {
                $return = $rtp->save($quotation);
                if ($return) {
                    echo ($v . '新增成功' . PHP_EOL);
                } else {
                    echo ($v . '新增失败' . PHP_EOL);
                }
            }
        }
    }

    /**
     * 团队F等级升级 
     */
    public function community_upgrade()
    {
        // 查询符合条件的用户
        $users = db('user')->field('id,tid,username,f_level,tpath,tid,order_sum')->select();
        $config = config('site');
        if ($users) {
            $users = user_shaxia($users);
            foreach ($users as &$v) {
                if (isset($v['sanxia'])) {
                    // 统计伞下总和
                    $count = user_order_count($users, $v['sanxia']);
                    if (($count >= $config['uplevel_f1']) && ($v['order_sum'] >= $config['uplevel_user_f1'])) {
                        $v['f_level'] = 1;
                    } else {
                        $v['f_level'] = 0;
                    }
                    $user = new User();
                    $ret = $user->allowField(true)->isUpdate()->saveAll($users);
                    if ($ret) {
                        echo ('F1更新成功' . PHP_EOL);
                    } else {
                        echo ('F1更新失败' . PHP_EOL);
                    }
                }
            }

            foreach ($users as $v) {
                up_f_level($v);
            }
            die('更新社区F等级升级');
        }
        die('查无用户');
    }

    /**
     * 接单收益
     */
    public function order_profit()
    {
        //获得当日凌晨的时间戳
        $today = strtotime(date("Y-m-d"), time());
        try {
            $order = db('order')->field('id,uid,bls,uname,price,current_profit,shouyi,zhouqi,zt,give,switch_token')->where('zt', 1)->where('lasttime', '<', $today)->lock(true)->select();
            $users_id  = array_column($order, 'uid');
            $users = db('user')->field('id,bls,bls_frozen,usdt,order_sum')->whereIn('id', $users_id)->lock(true)->select();
            $sysetm_log = [];
            $sysetm_log_zt = [];
            $sysetm_log_bls = [];
            if ($order) {
                foreach ($order as $k => &$v) {
                    // 是否到期时间
                    if (db('order')->where('id', $v['id'])->whereTime('overtime', '<', $today)->count()) {
                        // Order::update(['id' => $v['id'], 'zt' => 0]); 
                        $v['zt'] = 0;
                        $v['stoptime'] = time();
                    }

                    $v['lasttime'] = $today;
                    // 是否赠送
                    if ($v['give'] == 0) {
                        // 收益算法
                        $current_profit = round($v['bls'] * (1 / 100), 4);
                        $v['current_profit'] += $current_profit;
                    }

                    // 收益日志
                    foreach ($users as &$vv) {
                        if ($vv['id'] == $v['uid']) {
                            // 判断是否赠送合约，赠送不会产生收益
                            if ($v['give'] == 0) {
                                // 是否USDT转换BHD释放收益 give = 0 USDT 
                                if ($v['switch_token'] == 0) {

                                    $sysetm_log_bls[$k]['uid'] =  $v['uid'];
                                    $sysetm_log_bls[$k]['uname'] =  $v['uname'];
                                    $sysetm_log_bls[$k]['bizhong'] =  'bls_frozen';
                                    $sysetm_log_bls[$k]['money'] =  '-'.$current_profit;
                                    $sysetm_log_bls[$k]['before'] =  $vv['bls_frozen'];
                                    $vv['bls_frozen'] -= $current_profit;

                                    $sysetm_log_bls[$k]['after'] =  $vv['bls_frozen'];
                                    $sysetm_log_bls[$k]['memo'] =  '锁仓释放';
                                    $sysetm_log_bls[$k]['zt'] = 1;
                                    $sysetm_log_bls[$k]['type'] =  12;

                                    
                                    $sysetm_log[$k]['uid'] =  $v['uid'];
                                    $sysetm_log[$k]['uname'] =  $v['uname'];
                                    $sysetm_log[$k]['bizhong'] =  'bls';
                                    $sysetm_log[$k]['money'] =  $current_profit;
                                    $sysetm_log[$k]['before'] =  $vv['bls'];

                                    $vv['bls'] += $current_profit;
                                    $sysetm_log[$k]['after'] =  $vv['bls'];
                                    $sysetm_log[$k]['memo'] =  '接单收益';
                                    $sysetm_log[$k]['zt'] = 1;
                                    $sysetm_log[$k]['type'] =  4;


                                    

                                }

                                // 是否到期
                                if ($v['zt'] == 0) {
                                    // 扣除历史总业绩
                                    $vv['order_sum'] -= $v['price'];
                                    // 退回本金增加记录
                                    $sysetm_log_zt[$k]['uid'] =  $v['uid'];
                                    $sysetm_log_zt[$k]['uname'] =  $v['uname'];
                                    $sysetm_log_zt[$k]['bizhong'] =  'usdt';
                                    $sysetm_log_zt[$k]['money'] =  $v['price'];
                                    $sysetm_log_zt[$k]['before'] =  $vv['usdt'];
                                     // 退回本金
                                     $vv['usdt'] += $v['price'];
                                    $sysetm_log_zt[$k]['after'] =  $vv['usdt'];
                                    $sysetm_log_zt[$k]['memo'] =  '接单收益到期本金退回';
                                    $sysetm_log_zt[$k]['zt'] = 1;
                                    $sysetm_log_zt[$k]['type'] =  5;
                                    
                                }
                            }
                        }

                    }
                }

                $qianbaosysetmlog = new QianbaoSysetmLog;
                $modelorder = new Order;
                $modeluser = new User;
                $sysetm_logs = array_merge($sysetm_log_bls,$sysetm_log,$sysetm_log_zt);
                $a1 = $qianbaosysetmlog->saveAll($sysetm_logs, false);
                $a2 = $modelorder->isUpdate()->saveAll($order);
                $a3 = $modeluser->isUpdate()->saveAll($users);
                if (!$a1) {
                    throw new \Exception("合约收益释放失败", 1);
                }
                if (!$a2) {
                    throw new \Exception("接单收益发放失败", 2);
                }
                if (!$a3) {
                    throw new \Exception("用户更新失败", 3);
                }
            } else {
                echo ('暂无接单收益发放' . PHP_EOL);
                die;
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $th) {
            // 回滚事务
            Db::rollback();
            echo ($th->getMessage() . PHP_EOL);
            die;
        }
        echo ('接单收益发放成功' . PHP_EOL);
        die;
    }

    /**
     * 分享收益(弃用)
     */
    private function share_profit()
    {
        $scorelog = [];
        try {
            $users = db('user')->field('id,username,level,score')->where('level', '>', 0)->where(['status' => 'normal'])->lock(true)->select();
            // 分享收益规则一：有效分享（）人最多拿（）代
            $fx_a = config('site.fx_condition_a');
            // 分享收益规则二：VIP（）最多拿（）代
            $fx_b = config('site.fx_condition_b');
            if ($users) {
                foreach ($users as $k => &$v) {
                    // 第一代
                    $users_id = db('user')->where(['status' => 'normal'])->where('level', '>', 0)->where('tid', $v['id'])->column('id');
                    if ($users_id) {
                        $fx_t = $fx_a[count($users_id)];
                        $fx_vip = $fx_b[$v['level']];
                        // 判断最小代数取值,减一代百分百
                        if ($fx_t > $fx_vip) {
                            $dai = $fx_vip - 1;
                        } else {
                            $dai = $fx_t - 1;
                        }
                        $score_money = db('user_score_log')->where('type', 1)->whereIn('uid', $users_id)->whereTime('createtime', 'yesterday')->sum('money');
                        // $score_money = db('user_score_log')->where('type', 1)->whereIn('uid', $users_id)->sum('money');
                        $s_id = dai($v, $users_id, $dai);
                        if ($s_id) {
                            $s_score_money = db('user_score_log')->where('type', 1)->whereIn('uid', $s_id)->whereTime('createtime', 'yesterday')->sum('money');
                            // $s_score_money = db('user_score_log')->where('type', 1)->whereIn('uid', $s_id)->sum('money');
                            $money = $score_money + $s_score_money * (10 / 100);
                        } else {
                            $money = $score_money;
                        }
                        if ($money) {
                            $scorelog[$k]['uid'] = $v['id'];
                            $scorelog[$k]['uname'] = $v['username'];
                            $scorelog[$k]['money'] = $money;
                            $scorelog[$k]['before'] = $v['score'];
                            $v['score'] += $money;
                            $scorelog[$k]['after'] =  $v['score'];
                            $scorelog[$k]['memo'] =  '昨日分享收益';
                            $scorelog[$k]['type'] =  2;
                        }
                    }
                }
                if ($scorelog) {
                    $modelscorelog = new UserScoreLog;
                    $modeluser = new User;
                    $a1 = $modelscorelog->saveAll($scorelog, false);
                    $a2 = $modeluser->isUpdate()->saveAll($users);
                    if (!$a1) {
                        throw new \Exception("活力值新增失败", 1);
                    }
                    if (!$a2) {
                        throw new \Exception("用户活力值更新失败", 3);
                    }
                } else {
                    throw new \Exception('暂无分享收益发放');
                }
            } else {
                throw new \Exception('暂无分享收益发放');
            }
            // 提交事务
            Db::commit();
        } catch (\Exception $th) {
            // 回滚事务
            Db::rollback();
            echo ($th->getMessage() . PHP_EOL);
            die;
        }
        echo ('分享收益发放成功' . PHP_EOL);
        die;
    }

    /**
     * 奖金池
     */
    private function jcj()
    {
        // 达到F等级
        // $users = db('user')->field(true)->where('f_level', '>', 0)->where(['status' => 'normal'])->select();
        $users = db('user')->field(true)->where(['status' => 'normal'])->select();
        $config = config('site');
        $sysetm_log1 = [];
        $sysetm_log2 = [];
        $sysetm_log3 = [];
        $sysetm_log4 = [];
        if ($users) {
            foreach ($users as $k => $v) {
                if (qianbao_log_money($v['id'])) {
                    // 自身>=F1时
                    if ($v['f_level'] > 0) {
                        // 自身同时到达VIP4和>=F1时，奖1000USDT，只获得1次；
                        if ($v['level'] == 4) {
                            if (!db('qianbao_sysetm_log')->where(['uid' => $v['id'], 'type' => 4, 'zt' => '1', 'jcj_type' => 3])->count()) {
                                $sysetm_log1[$k]['uid'] =  $v['id'];
                                $sysetm_log1[$k]['uname'] =  $v['username'];
                                $sysetm_log1[$k]['bizhong'] =  'usdt';
                                $sysetm_log1[$k]['money'] =  $config['jcj3'];
                                $sysetm_log1[$k]['before'] =  $v['usdt'];
                                $sysetm_log1[$k]['after'] =  $v['usdt'] + $config['jcj3'];
                                $sysetm_log1[$k]['memo'] =  '到达VIP4和大于等于F1时，奖' . $config['jcj3'] . 'USDT';
                                $sysetm_log1[$k]['zt'] =  1;
                                $sysetm_log1[$k]['disable'] =  1;
                                $sysetm_log1[$k]['type'] =  4;
                                $sysetm_log1[$k]['jcj_type'] =  3;
                            }
                        }
                        // 自身>=F1时，直推出现VIP4（该直推VIP降级又升级后不复用），奖1000USDT，最多获得3次；
                        $vip_count = db('qianbao_sysetm_log')->where('uid', $v['id'])->where(['type' => 4, 'zt' => '1', 'jcj_type' => 4])->count();
                        if ($vip_count < 3) {
                            $user_tid = db('user')->where('tid', $v['id'])->where('level', 4)->column('id');
                            if ($user_tid) {
                                foreach ($user_tid as $v_tid) {
                                    if ($vip_count < 3) {
                                        if (!db('qianbao_sysetm_log')->where('uid', $v['id'])->where(['type' => 4, 'zt' => '1', 'jcj_type' => 4, 'memo' => $v_tid])->count()) {
                                            $sysetm_log2[$v_tid]['uid'] =  $v['id'];
                                            $sysetm_log2[$v_tid]['uname'] =  $v['username'];
                                            $sysetm_log2[$v_tid]['bizhong'] =  'usdt';
                                            $sysetm_log2[$v_tid]['money'] =  $config['jcj4'];
                                            $sysetm_log2[$v_tid]['before'] =  $v['usdt'];
                                            $sysetm_log2[$v_tid]['after'] =  $v['usdt'] + $config['jcj4'];
                                            $sysetm_log2[$v_tid]['memo'] =  $v_tid;
                                            $sysetm_log2[$v_tid]['zt'] =  1;
                                            $sysetm_log2[$v_tid]['disable'] =  1;
                                            $sysetm_log2[$v_tid]['type'] =  4;
                                            $sysetm_log2[$v_tid]['jcj_type'] =  4;
                                            $vip_count++;
                                        }
                                    }
                                }
                            }
                        }


                        // 首次达到F等级后获得，奖1000个USDT，F1-6每个等级只可获得1次，所以最多6次；
                        if (db('qianbao_sysetm_log')->where(['uid' => $v['id'], 'type' => 4, 'zt' => '1', 'jcj_type' => 5])->count() < 6) {
                            if (!db('qianbao_sysetm_log')->where(['uid' => $v['id'], 'type' => 4, 'zt' => '1', 'jcj_type' => 5, 'memo' => 'F' . $v['f_level']])->count()) {
                                $sysetm_log3[$k]['uid'] =  $v['id'];
                                $sysetm_log3[$k]['uname'] =  $v['username'];
                                $sysetm_log3[$k]['bizhong'] =  'usdt';
                                $sysetm_log3[$k]['money'] =  $config['jcj5'];
                                $sysetm_log3[$k]['before'] =  $v['usdt'];
                                $sysetm_log3[$k]['after'] =  $v['usdt'] + $config['jcj5'];
                                $sysetm_log3[$k]['memo'] =  'F' . $v['f_level'];
                                $sysetm_log3[$k]['zt'] =  1;
                                $sysetm_log3[$k]['disable'] =  1;
                                $sysetm_log3[$k]['type'] =  4;
                                $sysetm_log3[$k]['jcj_type'] =  5;
                            }
                        }
                    }

                    // 我有直推的用户，合约已完成，奖励自己1个USDT，可获得无限次
                    $users_tid = db('user')->where('tid', $v['id'])->column('id');
                    if ($users_tid) {
                        // 我的直推用户是否有完成的
                        $order_tid = db('order')->whereIn('uid', $users_tid)->where('zt', 0)->column('uid');
                        if ($order_tid) {
                            // 完成的直推合约用户，是否已经奖励过我自己
                            foreach ($order_tid as $o_tid) {
                                if (!db('qianbao_sysetm_log')->where(['uid' => $v['id'], 'type' => 4, 'zt' => '1', 'jcj_type' => 2, 'memo' => $o_tid])->count()) {
                                    // if (db('order')->where('uid', $v['id'])->where('zt', 0)->count()) {
                                    $sysetm_log4[$o_tid]['uid'] =  $v['id'];
                                    $sysetm_log4[$o_tid]['uname'] =  $v['username'];
                                    $sysetm_log4[$o_tid]['bizhong'] =  'usdt';
                                    $sysetm_log4[$o_tid]['money'] =  $config['jcj2'];
                                    $sysetm_log4[$o_tid]['before'] =  $v['usdt'];
                                    $sysetm_log4[$o_tid]['after'] =  $v['usdt'] + $config['jcj2'];
                                    $sysetm_log4[$o_tid]['memo'] =  $o_tid;
                                    $sysetm_log4[$o_tid]['zt'] =  1;
                                    $sysetm_log4[$o_tid]['disable'] =  1;
                                    $sysetm_log4[$o_tid]['type'] =  4;
                                    $sysetm_log4[$o_tid]['jcj_type'] =  2;
                                }
                            }
                        }
                    }
                }
            }
            $sysetm_log = array_merge($sysetm_log1, $sysetm_log2, $sysetm_log3, $sysetm_log4);
            if ($sysetm_log) {
                $qianbaosysetmlog = new QianbaoSysetmLog;
                if (!$qianbaosysetmlog->saveAll($sysetm_log, false)) {
                    echo ("奖金池分红新增失败");
                    die;
                } else {
                    echo ("奖金池分红新增成功");
                    die;
                }
            }
        }
        echo ('暂无奖金池分红' . PHP_EOL);
    }

    /**
     * 团队收益
     */

    public function community_profit()
    {   

        // 全部用户
        // 合约正在进行中
        $orders = db('order')->field(true)->where('zt', 1)->whereTime('createtime','yesterday')->select();
        // $orders = db('order')->field(true)->where('zt', 1)->select();
        $config = config('site');
        $qianbaosysetmlog = new QianbaoSysetmLog;
        $data = [];
        if ($orders) {
            foreach($orders as $order){
                // 启动事务
                $user = db('user')->field('tid,id,f_level,tcc,username,usdt')->where('id',$order['uid'])->where(['status' => 'normal'])->find();
                $data = ad($user,$order);
                if($data){
                    Db::startTrans();
                    try {
                    foreach($data['users'] as $v){
                        if (!db('user')->where('id', $v['id'])->update(['usdt' => $v['usdt']])) {
                            throw new \Exception("用户更新失败", 1);
                        }
                    }
                    if (!$qianbaosysetmlog->saveAll($data['qian_log'], false)) {
                        throw new \Exception("日志更新失败", 1);
                    }
                    // 提交事务
                    Db::commit();
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        echo ($e->getMessage() . PHP_EOL);die;
                    }
                    foreach($data['users'] as $v){
                        echo ('团队奖励分红成功' .json_encode($v). PHP_EOL);
                    }
                    foreach($data['qian_log'] as $v){
                        echo ('团队奖励分红_日志'.$v['memo'] .json_encode($v). PHP_EOL);
                    }
                }else{
                    echo ('暂无团队奖励分红' . PHP_EOL);
                }
            }
        }else{
            echo ('暂无团队奖励分红' . PHP_EOL);
        }
    }


    /**
     * 合伙人收益
     */
    public function profit_partner()
    {
        // 全部用户
        // 合约正在进行中
        $orders = db('order')->field(true)->where('zt', 1)->whereTime('createtime','yesterday')->select();
        $config = config('site');
        $qianbaosysetmlog = new QianbaoSysetmLog;
        $data = [];
        if ($orders) {
            foreach($orders as $order){
                // 启动事务
                $user = db('user')->field('tid,id,username,usdt')->where('id',$order['uid'])->where(['status' => 'normal'])->find();
                $data = profit_partner($user,$order);
                if($data){
                    // dump($data);die;
                    Db::startTrans();
                    try {
                    foreach($data['users'] as $v){
                        if (!db('user')->where('id', $v['id'])->update(['usdt' => $v['usdt']])) {
                            throw new \Exception("用户更新失败", 1);
                        }
                    }
                    if (!$qianbaosysetmlog->saveAll($data['qian_log'], false)) {
                        throw new \Exception("日志更新失败", 1);
                    }
                    // 提交事务
                    Db::commit();
                    } catch (\Exception $e) {
                        // 回滚事务
                        Db::rollback();
                        echo ($e->getMessage() . PHP_EOL);die;
                    }
                    foreach($data['users'] as $v){
                        echo ('合伙人奖励分红成功' .json_encode($v). PHP_EOL);
                    }
                    foreach($data['qian_log'] as $v){
                        echo ('合伙人奖励分红_日志'.$v['memo'] .json_encode($v). PHP_EOL);
                    }
                }else{
                    echo ('暂无合伙人奖励分红' . PHP_EOL);
                }
            }
        }else{
            echo ('暂无合伙人奖励分红' . PHP_EOL);
        }
    }

    /**
     * 实际获取eht标记手续费
     */
    public function eth_kgf()
    {
        $eth_kgf = apigasPrice(2);
        if($eth_kgf){
            $id = db('rtp')->where('type','kgf')->value('id');
            if($id){
                $data['id'] = $id;
                $data['price'] = $eth_kgf;
                $data['updatetime'] = time();
                if(db('rtp')->update($data)){
                    die('eth标记手续费更新成功');
                }
                die('eth标记手续费更新失败');
            }else{
                $data['type'] = 'kgf';
                $data['price'] = $eth_kgf;
                $data['createtime'] = time();
                if(db('rtp')->insert($data)){
                    die('eth标记手续费添加成功');
                }
                die('eth标记手续费添加失败');
            }
        }
        die('查询失败');
    }

    /**
     * USDT自动转账
     */
    public function usdt_automatic()
    {
        $qian = new \app\admin\controller\QianbaoSysetmLog;
        $id = db('qianbao_sysetm_log')->where('type',2)->where('zt',5)->value('id');
        if(!$id){
          $this->error('暂无自动转账需求'); 
        }
        $qian->withdraw_money_request($id);


    }


    /**
     * 归集链上状态自动检测更新
     */
    public function piliang_cha_guiji_daozhang(){
        set_time_limit(0);
        $lists=db('qianbao_sysetm_log')->where('type',20)->where('zt',3)->select();
        // dump($lists);die;
        if($lists){
            foreach ($lists as $list) {
                Db::startTrans();
                try {
                    $ret = erc_daozhang($list['hash']);
                    if($ret['zt']==1){
                        $ret_log = db('qianbao_sysetm_log')->where('id',$list['id'])->update(['zt'=>1,'memo'=>$ret['msg']]);
                        $ret_last_num = db('qianbao_sysetm_addr')->where('uid',$list['uid'])
                            ->inc($list['bizhong'].'_last_num',$list['money'])->update([$list['bizhong'].'_last_time'=>time()]);
                        if(!$ret_log && !$ret_last_num){
                            throw new \Exception("到账成功，数据未更新",1);
                        }else{
                            echo $list['uname'].'：'.$ret['msg'].'<hr>';
                        }
                    }elseif($ret['zt']==2){
                        $ret_log = db('qianbao_sysetm_log')->where('id',$list['id'])->update(['zt'=>2,'memo'=>$ret['msg']]);
                        if(!$ret_log){
                            throw new \Exception("到账失败，数据未更新",2);
                        }else{
                            echo $list['uname'].'：'.$ret['msg'].'<hr>';
                        }
                    }else{
                        $ret_log = db('qianbao_sysetm_log')->where('id',$list['id'])->update(['memo'=>$ret['msg'],'time_cl' => time()]);
                        if(!$ret_log){
                            throw new \Exception("交易中或查询失败，数据未更新".$ret['zt'],3);
                        }else{
                            echo $list['uname'].'：'.$ret['msg'].'<hr>';
                        }
                        // echo $list['id'].'>>>'.$ret['msg'].'<hr>';
                    }
                    Db::commit();  
                } catch (\Exception $e) {
                    Db::rollback();
                    echo $e->getMessage();
                    $this->error($e->getMessage());
                    // $this->error($e->getCode());
                }
            }
        }else{
            echo '暂无需要更新的数据';
        }
    }
}
